package com.qq2008.game.mojie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qq2008.game.mojie.jdo.Friend;
import com.qq2008.game.mojie.jdo.Role;
import com.qq2008.game.mojie.jdo.User;
import com.qq2008.game.mojie.mapper.FriendMapper;
import com.qq2008.game.mojie.mapper.RoleMapper;
import com.qq2008.game.mojie.mapper.UserMapper;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/mojie")
public class FriendController extends BaseController {

    @Resource
    private FriendMapper friendMapper;

    @Resource
    private RoleMapper roleMapper;
    /**
     * @param model
     * @param session
     * @param friendId 需要添加好友的id
     * @return
     */
    @RequestMapping("addToFriend")
    public String addToFriend(Model model, HttpSession session, String friendId) {
        Integer flag = 0;
        Role role = getSessionRole(session);
        List<Friend> myFriends = friendMapper.selectList(new LambdaQueryWrapper<Friend>().eq(Friend::getBelongId, role.getUserId()));
        for (Friend myFriend : myFriends) {
            if (Integer.parseInt(friendId) == myFriend.getUserId()) {
                model.addAttribute("is", "您已经添加过该玩家为好友了哦！");
                flag = 1;
            }
        }
        if (flag == 0) {
            model.addAttribute("role", role);
            model.addAttribute("friendId", friendId);
        }
        if (myFriends.size() == 0) {
            model.addAttribute("role", role);
            model.addAttribute("friendId", friendId);
        }
        return "addFriend";
    }

    /**
     *
     * @param model
     * @param session
     * @param friendId
     * @param input 申请好友原因（预留，暂未开发）
     * @return
     */
    @ResponseBody
    @PostMapping("addFriend")
    @Transactional(rollbackFor = Exception.class)
    public String addf(Model model, HttpSession session, String friendId, String input) {
        Role role = getSessionRole(session);
        //加对方为好友
        Friend friend = new Friend();
        friend.setBelongId((long) role.getUserId());
        friend.setUserId(Long.parseLong(friendId));
        Role user = roleMapper.get(Integer.parseInt(friendId) );
        friend.setUserName(user.getName());

        LambdaQueryWrapper<Friend> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Friend::getBelongId, (long) role.getUserId());
        queryWrapper.eq(Friend::getUserId, (Long.parseLong(friendId)));
        Long isFriend = friendMapper.selectCount(queryWrapper);
        if (isFriend == 0) {
            //申请好友start
            int insert = friendMapper.insert(friend);
            //申请好友end
            //对方自动添加自己（后期优化成审核）
            Friend friendAgain = Friend.builder()
                    .belongId(Long.parseLong(friendId))
                    .userId((long) role.getUserId())
                    .userName(role.getName())
                    .build();
            friendMapper.insert(friendAgain);
            if (insert == 1) {
                return "200";//申请成功
            }
        } else {
            return "201";//已申请过了
        }
        return null;
    }


    /**
     *
     * @param model
     * @param session
     * @return查询好友列表
     */
    @RequestMapping("myfriend")
    public String selectFriend(Model model, HttpSession session){
        Role role = getSessionRole(session);
        List<Friend> friends = friendMapper.selectList(new LambdaQueryWrapper<Friend>().eq(Friend::getBelongId, role.getUserId()));
        model.addAttribute("myFriends",friends);
        return "myfriend";
    }
}
